// pages/orderlist/orderlist.js
const order = require('../../api/order.js')
const personal = require('../../api/personal.js')
const product = require('../../api/product.js')
Page({

  /**
   * 页面的初始数据
   */
  data: {
    index: 0,
    order: [],
    order1: [],
    order2: [],
    order3: [],
    user: {},
    product: []
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    let index = options.index
    this.setData({
      index: index
    })
    wx.showLoading({
      title: '正在加载',
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  // 获取订单
  getOrders() {
    console.log(this.data.index)
    order.getOrders().then(res => {
      if (res.data.code == 0) {
        let order = []
        let order1 = []
        let order2 = []
        let order3 = []
        res.data.data.forEach(value => {
          console.log(value.common_user.id, this.data.user.id)
          if (value.common_user.id == this.data.user.id) {
            order3.push(value)
            if (value.status == 0) {
              order.push(value)
            } else if (value.status == 1) {
              order1.push(value)
            } else if (value.status == 2 || value.status == 3) {
              order2.push(value)
            }
          }
        })
        this.setData({
          order: order,
          order1: order1,
          order2: order2,
          order3: order3
        })
        wx.hideLoading()
      }
    })
  },
  // 获取用户信息
  getUserInfo(token) {
    personal.getUserInfo(token).then(res => {
      if (res.data.code == 0) {
        this.setData({
          user: res.data.data
        })
        wx.hideLoading()
      }
    })
  },
  onChangeIndex(e) {
    this.setData({
      index: e.detail.index
    })
    console.log(this.data.index)
  },
  onShow: function() {
    let token = wx.getStorageSync('token')
    this.getUserInfo(token)
    this.getOrders()
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {

  },
  // 取消订单
  cancelOrder(e) {
    let id = e.target.dataset.orderid
    let inventory = 0
    let newInventory = 0
    let productid = 0
    let count = 0
    let sale_count = 0
    let data2 = {}
    let data = {
      status: 3
    }
    order.updateOrderStatus(id, data).then(res => {
      if (res.data.code == 0) {
        wx.showToast({
          title: '订单已取消',
        })
        order.getOrderById(id).then(res => {
          inventory = res.data.data[0].product.inventory
          productid = res.data.data[0].product.id
          sale_count = res.data.data[0].product.sale_count
          count = res.data.data[0].count
          newInventory = parseInt(inventory) + parseInt(count)
          data2.inventory = newInventory
          data2.sale_count = parseInt(sale_count) - parseInt(count)
          product.updateInventory(productid, data2).then(res => {
            console.log('订单取消库存发生改变')
          })
        })
        this.getOrders()
      }
    })
  },
  // 确认收货订单
  confirmOrder(e) {
    let id = e.target.dataset.orderid
    let data = {
      status: 2
    }
    order.updateOrderStatus(id, data).then(res => {
      if (res.data.code == 0) {
        wx.showToast({
          title: '收货成功',
        })
        this.getOrders()
      }
    })
  },
  // 删除订单
  deleteOrder(e) {
    let id = e.target.dataset.orderid
    let data = {
      status: 4
    }
    order.updateOrderStatus(id, data).then(res => {
      if (res.data.code == 0) {
        wx.showToast({
          title: '订单已删除',
        })
        this.getOrders()
      }
    })
  },
  // 去付款
  toPay(e) {
    console.log(e)
    let id = e.target.dataset.orderid
    console.log(id)
    wx.navigateTo({
      url: '../order/orderpay/orderpay?id=' + id
    })
  },
  toOrderDetail(e) {
    let id = e.currentTarget.dataset.id
    wx.navigateTo({
      url: './orderdetail/orderdetail?id=' + id,
    })
  }
})